package com.cskaoyan.utils;

import org.apache.commons.dbcp.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class DBCPUtils {

    // 数据库连接池对象本身
    static DataSource dataSource;

    static {

        try {
            Properties properties = new Properties();
            FileInputStream fileInputStream = new FileInputStream("dbcp.properties");
            properties.load(fileInputStream);

            // 初始化数据库连接池
            dataSource = BasicDataSourceFactory.createDataSource(properties);
        }catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    // 获取连接的方法
    public static Connection getConnection(){

        Connection connection = null;
        try {

            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return connection;

    }
}
